package com.youyou.fusheng2.View.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.gson.Gson;
import com.youyou.fusheng2.Moudle.FuShengCallBack;
import com.youyou.fusheng2.Moudle.OnClick;
import com.youyou.fusheng2.Moudle.OnClick2;
import com.youyou.fusheng2.Person.FuShengMoudle;
import com.youyou.fusheng2.Person.Test.HomeBannerTest;
import com.youyou.fusheng2.Person.Test.HomeTedayTest;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.EveryUrl;
import com.youyou.fusheng2.View.Activity.Sousuo_activity;
import com.youyou.fusheng2.View.Adapter.SouSuoLiShiAdapter;
import com.youyou.fusheng2.View.Adapter.SouSuoPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SousuoFragment extends Fragment implements TextView.OnEditorActionListener, FuShengCallBack, OnClick, OnClick2 {

    private TextView                  home_tetle;
    private ImageView                 home_sousuo;
    private EditText                  home_sousuo_input;
    private RelativeLayout            layout;
    private RecyclerView              sousuotuijian;
    private RecyclerView              sousuolishi;
    private RecyclerView              sousuo_rec;
    private List<String>              ListItem      = new ArrayList<>();
    private List<String>              tuijianList   = new ArrayList<>();
    private List<Map<String, String>> typeTestLists = new ArrayList<>();
    private Map<String, String>       typeTestmap;
    private SouSuoLiShiAdapter souSuoLiShiAdapter1;
    private SouSuoLiShiAdapter souSuoLiShiAdapter2;
    private MyHander           myHander      = new MyHander();
    private FuShengMoudle      fuShengMoudle = new FuShengMoudle(this);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        View inflate = inflater.inflate(R.layout.sousuo_fragment, container,false);
        initView(inflate);
        initViewLiShi();
        GetTuijian();
        initViewTuiJian();
        initViewPager();
        return inflate;
    }

    @SuppressLint("ResourceAsColor")
    private void initViewPager() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:17
         *  @简介: sousuo_rec
         */
        SouSuoPagerAdapter souSuoPagerAdapter = new SouSuoPagerAdapter(typeTestLists,this);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
        sousuo_rec.setAdapter(souSuoPagerAdapter);
        sousuo_rec.setLayoutManager(layoutManager);
        souSuoPagerAdapter.notifyDataSetChanged();
    }

    private void GetTuijian() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                fuShengMoudle.getUrlDate(EveryUrl.bannerUrl, 0);
                fuShengMoudle.getUrlDate(EveryUrl.vdioType2, 1);
            }
        }).start();
    }


    private void initViewTuiJian() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  00:53
         *  @简介: 初始化搜索历史rec
         */
        souSuoLiShiAdapter2 = new SouSuoLiShiAdapter(2, ListItem, (OnClick) this);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
        sousuotuijian.setLayoutManager(staggeredGridLayoutManager);
        sousuotuijian.setAdapter(souSuoLiShiAdapter2);
    }


    private void initViewLiShi() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  00:53
         *  @简介: 初始化搜索历史rec
         */
        souSuoLiShiAdapter1 = new SouSuoLiShiAdapter(1, ListItem, (OnClick) this);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
        sousuolishi.setLayoutManager(staggeredGridLayoutManager);
        sousuolishi.setAdapter(souSuoLiShiAdapter1);
        souSuoLiShiAdapter1.notifyDataSetChanged();
    }


    private void initView(View inflate) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  00:15
         *  @简介: 初始化布局 隐藏不必要的控件
         */
        sousuotuijian = inflate.findViewById(R.id.sousuo_tuijian);
        sousuolishi = inflate.findViewById(R.id.sousuo_lishi);
        home_tetle = inflate.findViewById(R.id.home_tetle);
        home_tetle.setVisibility(View.GONE);
        home_sousuo = inflate.findViewById(R.id.home_sousuo);
        home_sousuo.setVisibility(View.GONE);
        home_sousuo_input = inflate.findViewById(R.id.home_sousuo_input);
        sousuo_rec = inflate.findViewById(R.id.sousuo_viewpager);
        home_sousuo_input.setVisibility(View.VISIBLE);
        home_sousuo_input.setOnEditorActionListener(this);
        layout = inflate.findViewById(R.id.layout);
        ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
        layoutParams.height = (int) (layoutParams.height * 1.3);
        layout.setLayoutParams(layoutParams);
    }


    @Override
    public void GetSuccess(String text, int mark) {
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void GetError(String text, int mark) {
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void listener(String s, int i) {
        if (!s.isEmpty()) {
            Intent intent = new Intent(getContext(), Sousuo_activity.class);
            Bundle bundle = new Bundle();
            bundle.putString("text", s);
            intent.putExtra("id", bundle);
            startActivity(intent);
        } else {
            Toast.makeText(getContext(), "数据为空！", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void listener1(int Flage, String Text) {
        Bundle bundle = new Bundle();
        bundle.putInt("f", Integer.parseInt(Text));
        getContext().startActivity(new Intent(getContext(), com.youyou.fusheng2.View.Activity.playVdioActivity.class).putExtra("text", bundle));
    }

    @Override
    public void listener1(int Flage, String Text, int Index) {

    }

    @SuppressLint("HandlerLeak")
    public class MyHander extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    JieXi(msg.obj.toString());
                    souSuoLiShiAdapter2.setListItem(tuijianList);
                    break;
                case 1:
                    ListBanner(msg.obj.toString());
                    break;
            }
        }
    }

    private void ListBanner(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  20:37
         *  @简介: 解析sousuo页面底部
         */
        Gson gson = new Gson();
        HomeTedayTest homeTedayTest = gson.fromJson(toString, HomeTedayTest.class);
        if (homeTedayTest.getCode() == 0) {
            List<HomeTedayTest.DataBean> data = homeTedayTest.getData();
            for (int i = 0; i < data.size(); i++) {
                typeTestmap = new HashMap<>();
                typeTestmap.put("pic", data.get(i).getV_pic());
                typeTestmap.put("tid", String.valueOf(data.get(i).getV_id()));
                typeTestmap.put("tname", String.valueOf(data.get(i).getV_name()));
                typeTestLists.add(typeTestmap);
            }
        }
    }

    private void JieXi(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:09
         *  @简介: 解析的是默认的轮播 设置为推荐搜索
         */
        Gson gson = new Gson();
        HomeBannerTest homeBannerTest = gson.fromJson(toString, HomeBannerTest.class);
        List<HomeBannerTest.DataBean> data = homeBannerTest.getData();
        for (int i = 0; i < data.size(); i++) {
            tuijianList.add(data.get(i).getV_name());
        }
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/25  02:12
         *  @简介: 把键盘设置为搜索健
         *  根据i判断IME_ACTION_SEARCH
         */
        if (i == EditorInfo.IME_ACTION_SEARCH) {
            if (ListItem.size() >= 8) {
                ListItem.remove(0);
                ListItem.remove(1);
                ListItem.remove(2);
                ListItem.remove(3);
            }
            if (!(home_sousuo_input.getText().toString().trim().isEmpty())) {
                String trim = home_sousuo_input.getText().toString().trim();
                ListItem.add(trim);
                initViewLiShi();
                Intent intent = new Intent(getContext(), Sousuo_activity.class);
                Bundle bundle = new Bundle();
                bundle.putString("text", home_sousuo_input.getText().toString().trim());
                intent.putExtra("id", bundle);
                startActivity(intent);
                home_sousuo_input.setText("");
            }
            return true;
        }
        return false;
    }

}